package packageHandler;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.List;
import java.util.Map;

import javax.swing.JOptionPane;

import protocol.DataPackage;
import protocol.JsonPackage;
import util.DBUtil;

import com.fasterxml.jackson.databind.ObjectMapper;

public class PackageHandlerForServer {
	public static void clientLogin(Map<String, BufferedWriter> socketList, List<String> userList, 
			DataPackage pack, BufferedWriter writers) throws IOException{
//		String loginName = pack.getFromName();
//		String password = pack.getMessageData();
//		
//		if(DBUtil.checkUser(loginName)){
//			if(DBUtil.checkPasswork(loginName, password)){
//				if(!DBUtil.checkIsLogin(loginName)){
//					if(DBUtil.login(loginName)){
//						writers.write("202\n");
//						writers.flush();
//					}
//					else{
//						writers.write("303\n");	
//						writers.flush();
//					}
//				}
//				else{
//					writers.write("404\n");	//用户在线
//					writers.flush();
//				}
//			}
//			else{
//				writers.write("505\n");	//用户密码
//				writers.flush();
//			}
//		}
//		else{
//			writers.write("000\n");	//用户不存在
//			writers.flush();
//		}		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		//维护用户列表 和 socketList
		socketList.put(pack.getFromName(), writers);
		userList.add(pack.getFromName());
		
		//将特定的确认登录包返回给登录者
		String fromName = pack.getFromName();
		BufferedWriter o = writers;
		
		DataPackage loginPackage = new DataPackage("\\", fromName, DataPackage.MessageType.LOGIN, (fromName + " login success "), System.currentTimeMillis());
		JsonPackage loginJsonPackage = new JsonPackage(loginPackage);
		try {
//			o.writeObject(loginPackage);
			o.write(loginJsonPackage.getJsonString() + "\n");
			o.flush();
		} catch (IOException e) {
			System.out.println("向目标用户" + fromName + "发送登录信息失败");
			e.printStackTrace();
		}
		
		if(userList.size() != 0){						//如果还没有用户登录就不用群发登录列表
			//发送用户列表给所有用户
			String userListString = "";
			for(String i:userList){ 
				userListString += i;
				userListString += "\\";
			}
			userListString = userListString.substring(0, userListString.length() - 1);
			
			//向所有用户发送 用户列表
			DataPackage userListPackage = new DataPackage("\\", "\\", DataPackage.MessageType.LIST, userListString, 0);
			JsonPackage userListJsonPackage = new JsonPackage(userListPackage);
			//发送某个用户登录的提示消息
			DataPackage user= new DataPackage("\\", "\\", DataPackage.MessageType.LOGIN, (fromName + " login... "), System.currentTimeMillis());
			JsonPackage userJsonPackage = new JsonPackage(user);
			
			for(String i:userList){ 
				BufferedWriter out = socketList.get(i);
				try {
					out.write(userListJsonPackage.getJsonString()  + "\n");
					out.flush();
					if(!i.equals(fromName)){
						out.write(userJsonPackage.getJsonString()  + "\n");
						out.flush();
					}
				} catch (IOException e) {
					System.out.println("用户登录，服务器向所有用户发送用户列表失败");
					e.printStackTrace();
				}
			}
		}
	}		
	
	public static void clientLogout(Map<String, BufferedWriter> socketList, List<String> userList, DataPackage pack){
		//用户下线，将其移出socketList
		
		System.out.println("server know " + pack.getFromName() + " is logout...");
		String fromName = pack.getFromName();
		
		//提示该用户下线
		DataPackage logoutPackage = new DataPackage("\\", fromName, DataPackage.MessageType.LOGOUT, fromName + " logout sucess...", 0);
		BufferedWriter out1 = socketList.get(fromName);
		try {
			out1.write(new JsonPackage(logoutPackage).getJsonString() + "\n");
			out1.flush();
		} catch (IOException e1) {
			System.out.println("服务端提示用户下线异常");
		}
		
		//将该用户移除
		socketList.remove(fromName);
		userList.remove(fromName);
		
		if(userList.size() != 0){					//如果已经没有在线的用户，就不用群发登录列表		
			//发送用户列表给所有用户
			String userListString = "";
			for(String i:userList){ 
				userListString += i;
				userListString += "\\";
			}
			userListString = userListString.substring(0, userListString.length() - 1);
			
			DataPackage userListPackage = new DataPackage("\\", "\\", DataPackage.MessageType.LIST, userListString, 0);
			DataPackage userKnow = new DataPackage("\\", "\\", DataPackage.MessageType.LOGOUT, pack.getMessageData(), System.currentTimeMillis());
			//向所有用户发送 用户列表//通知其他用户该用户下线
			for(String i:userList){
				BufferedWriter out = socketList.get(i);
				try {
					out.write(new JsonPackage(userListPackage).getJsonString() + "\n");
					out.flush();
					out.write(new JsonPackage(userKnow).getJsonString() + "\n");
					out.flush();
				} catch (IOException e) {
					System.out.println("用户下线，服务端向所有用户发送用户列表失败");
					e.printStackTrace();
				}
			}
		}
	}
	
	public static void transmitMassageForClient(Map<String, BufferedWriter> socketList, List<String> userList, DataPackage pack){
		//将包扔到目标用户
		
		String toName = pack.getToName();
		
		if(toName.equals("\\")){//群发
			for(String i:userList){
				if(i.equals(pack.getFromName())){
					;
				}
				else{
					BufferedWriter o = socketList.get(i);
					try {
						o.write(new JsonPackage(pack).getJsonString() + "\n");
						o.flush();
					} catch (IOException e) {
						System.out.println("群发出现异常");
						e.printStackTrace();
					}
				}
			}
		}
		BufferedWriter o = socketList.get(toName);
		try {
			if(o != null){
				o.write(new JsonPackage(pack).getJsonString()+ "\n");
				o.flush();
				System.out.println("找到了用户 " + toName);
			}
			else{
				System.out.println("找不到用户  " + toName);
			}
		} catch (IOException e) {
			System.out.println(pack.getFromName() + "向目标用户" + toName + "发送信息失败");
			e.printStackTrace();
		}
	}
}
